<template>
    <div>
        <breadcrumb :routerList="routerList"></breadcrumb>
        <div class="form-table">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="旧密码" prop="old_pwd">
                    <el-input v-model="ruleForm.old_pwd" class="input" type="password"></el-input>
                </el-form-item>
                <el-form-item label="新密码" prop="new_pwd">
                    <el-input v-model="ruleForm.new_pwd" class="input" type="password"></el-input>
                </el-form-item>
                <el-form-item label="确认新密码" prop="confirm_pwd">
                    <el-input v-model="ruleForm.confirm_pwd" class="input" type="password"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    import breadcrumb from '@/components/breadcrumb.vue'
    export default {
        components:{breadcrumb},
        data() {
            return {
                routerList:[
                    {
                        path:'',
                        name:'修改密码'
                    }
                ],
                ruleForm: {
                    old_pwd: '',
                    new_pwd: '',
                    confirm_pwd: ''
                },
                rules: {
                    old_pwd: [
                        { required: true, message: '请输入旧密码', trigger: 'blur' },
                        { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
                    ],
                    new_pwd: [
                        { required: true, message: '请输入新密码', trigger: 'blur' },
                        { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
                    ],
                    confirm_pwd: [
                        { required: true, message: '请输入确认密码', trigger: 'blur' },
                        { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
                    ],

                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if(this.ruleForm.new_pwd == this.ruleForm.confirm_pwd){
                            this.resetPwd()
                        }else{
                            this.$message.warning('两次密码输入不一致，请重新输入！')
                            this.ruleForm.new_pwd = '';
                            this.ruleForm.confirm_pwd = '';
                        }
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            async resetPwd(){
                let res = await this.$Http.resetPwd({
                    user_name:this.$store.state.user_name,
                    user_pwd:this.ruleForm.old_pwd,
                    new_user_pwd:this.ruleForm.new_pwd
                })
                console.log(res);
                if(res.code == 200){
                    this.$message.success(res.log);

                }else if(res.code == 400){
                    this.$message.warning(res.log)
                }
                this.ruleForm.new_pwd = '';
                this.ruleForm.old_pwd = '';
                this.ruleForm.confirm_pwd = '';
            }
        }
    }
</script>

<style scoped>
    .form-table{
        width: 100%;
        background-color: #fff;
        box-sizing: border-box;
        padding:20px;
        margin-top: 20px;
    }
    .input{
        width: 50%;
    }
</style>
